<template>
  <div>
    <el-table
      :data="filteredData"
      style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="课程名">
              <span>{{ props.row.courseName }}</span>
            </el-form-item>
            <el-form-item label="申请教师">
              <span>{{ props.row.teacher }}</span>
            </el-form-item>
            <el-form-item label="上课时间">
              <span>{{ getCourseTime([props.row.weekday, props.row.startTime, props.row.endTime]) }}</span>
            </el-form-item>
            <el-form-item label="上课班级">
              <span>{{ props.row.clazzList.toString() }}</span>
            </el-form-item>
            <el-form-item label="教室">
              <span>{{ props.row.room }}</span>
            </el-form-item>
            <el-form-item label="审核">
              <el-tag v-if="props.row.status !== 'wait'" :type="tagType(props.row.status)">{{ tagName(props.row.status) }}</el-tag>
              <el-button v-else size="small" type="primary" plain @click="handleClickEdit(props.row)">编辑</el-button>
            </el-form-item>
            <el-form-item label="备注">
              <span>{{ props.row.remarks }}</span>
            </el-form-item>
            <el-form-item label="审核员" v-if="props.row.status !== 'wait'">
              <span>{{ props.row.inspector }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column
        label="课程名"
        prop="courseName">
      </el-table-column>
      <el-table-column
        label="申请教师"
        prop="teacher">
      </el-table-column>
      <el-table-column
        label="状态"
        prop="status">
        <template slot="header" slot-scope="scope">
          <el-input
            v-model="search"
            size="mini"
            placeholder="搜索"/>
        </template>
        <template slot-scope="scope">
          <el-tag
            :type="tagType(scope.row.status)"
            disable-transitions>{{tagName(scope.row.status)}}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="pagination"
      layout="prev, pager, next"
      :total="50">
    </el-pagination>
    <el-dialog title="审核申请" :visible.sync="editDialog.visibility">
      <el-form>
        <el-form-item label="课程名">
          <span>{{tableData[this.editDialog.index].courseName}}</span>
        </el-form-item>
        <el-form-item label="申请教师">
          <span>{{tableData[this.editDialog.index].teacher}}</span>
        </el-form-item>
        <el-form-item label="班级">
          <span>{{tableData[this.editDialog.index].clazzList.toString()}}</span>
        </el-form-item>
        <el-form-item label="上课教室">
          <span>{{tableData[this.editDialog.index].room}}</span>
        </el-form-item>
        <el-form-item label="备注（可选）">
          <el-input type="textarea" :rows="5" placeholder="请输入备注" v-model="editDialog.remarks"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="float: right" @click="handleApprove">同意</el-button>
          <el-button type="danger" style="float: left" @click="handleReject">拒绝</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style>
  .demo-table-expand {
    font-size: 0;
  }

  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }

  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }

</style>

<script>
  import staticList from '@/staticData/courseApprove'
  import courseConfig from '@/components/Course/Table/config'

  const STATUS_TAG_TYPE_MAPPER = {
    wait: 'warning',
    approve: 'success',
    rejected: 'danger'
  }
  const STATUS_TAG_TEXT_MAPPER = {
    wait: '未审批',
    approve: '已通过',
    rejected: '已拒绝'
  }
  export default {
    data() {
      return {
        tableData: staticList,
        search: '',
        editDialog: {
          visibility: false,
          index: 0,
          id: 0,
          remarks: '',
        }
      }
    },
    methods: {
      handleApprove() {
        this.editDialog.visibility=false;
      },
      handleReject() {
        this.editDialog.visibility=false;

      },
      handleClickEdit(el) {
        this.editDialog = {
          visibility: true,
          id: el.id,
          index: this.tableData.indexOf(el),
          remarks: ''
        }
      },
      getCourseTime([weekday, startTime, endTime]) {
        return Object.values(courseConfig.WEEKDAY_MAPPER)[weekday] + '  ' + courseConfig.COURSE_START_SCHEDULE[startTime] + '-' + courseConfig.COURSE_END_SCHEDULE[endTime]
      },
      tagName: function(val) {
        return STATUS_TAG_TEXT_MAPPER[val]
      },
      tagType: function(val) {
        return STATUS_TAG_TYPE_MAPPER[val]
      }
    },
    computed: {
      filteredData: function() {
        return this.tableData.filter((item)=>{
          return (item.courseName.indexOf(this.search) !== -1) || (item.teacher.indexOf(this.search) !== -1)
        });
      }
    }
  }
</script>
